import 'dart:math';

import '../common_button.dart';
import 'index.dart';
import 'package:flutter/material.dart';

class FilterMultiplePage extends StatefulWidget with FilterPopupBaseWidget {
  static final GlobalKey<_FilterMultipleState> _globalKey =
      GlobalKey<_FilterMultipleState>();

  Offset offsetLT;
  FilterItemData itemData;
  SelectCallback onSelect;
  DismissCallback onDismiss;

  FilterMultiplePage(
      {required this.itemData,
      required this.onSelect,
      required this.onDismiss,
      this.offsetLT = Offset.zero})
      : super(key: _globalKey);

  @override
  _FilterMultipleState createState() => _FilterMultipleState();

  @override
  dismiss() {
    _FilterMultipleState? state = _globalKey.currentState;
    state?.dismiss();
  }
}

class _FilterMultipleState extends State<FilterMultiplePage>
    with SingleTickerProviderStateMixin {
  late FilterItemData _itemData;

  late Animation<Offset> _animation;
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    //对象深拷贝
    _itemData = FilterItemData.fromJson(widget.itemData.toJson());
    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 200),
    );
    _animation = Tween<Offset>(begin: const Offset(0, -1), end: Offset.zero)
        .animate(_controller);
    _controller.forward();
  }

  dismiss() {
    widget.onDismiss();
    _controller.reverse();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        padding: EdgeInsets.only(top: widget.offsetLT.dy),
        child: ClipRect(
          child: SlideTransition(
            position: _animation,
            child: GestureDetector(
              onTap: () {
                //阻断内容部分点击关闭效果
              },
              child: Container(
                constraints: BoxConstraints(maxHeight: 400),
                color: Theme.of(context).colorScheme.background,
                child: Column(children: [
                  Container(
                    constraints: BoxConstraints(maxHeight: 350),
                    child: ListView.builder(
                        padding: EdgeInsets.zero,
                        shrinkWrap: true,
                        itemCount: _itemData.children!.length,
                        itemExtent: 50,
                        itemBuilder: (context, index) {
                          FilterItemData itemData = _itemData.children![index];
                          return ListTile(
                            title: Text(itemData.name),
                            trailing: itemData.isSelect()
                                ? Icon(
                                    Icons.check_box,
                                    color: Theme.of(context).primaryColor,
                                  )
                                : const Icon(Icons.check_box_outline_blank),
                            onTap: () {
                              setState(() {
                                itemData.setSelect(!itemData.isSelect());
                              });
                            },
                          );
                        }),
                  ),
                  Row(
                    children: [
                      const SizedBox(
                        width: 5,
                      ),
                      Expanded(
                        child: CommonButton.outlinedButton(context, text: "取消",
                            onPressed: () {
                          //还原选中数据
                          filterPop(context);
                        }),
                      ),
                      const SizedBox(
                        width: 20,
                      ),
                      Expanded(
                        child: CommonButton.elevatedButton(context, text: "确定",
                            onPressed: () {
                          widget.onSelect(_itemData);
                          filterPop(context);
                        }),
                      ),
                      const SizedBox(
                        width: 5,
                      ),
                    ],
                  ),
                ]),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
